/*
 * JS for Social Network - Utrinki
 */

/* -- GLOBAL VARIABLES -- */


/* -- jQuery UI INITIALIZATION -- */
$(document).ready(function(){
	$("#registration_dialog").dialog({
		autoOpen: false,
		height: 550,
		width: 520,
		modal: true,
		buttons: {
			"Registracija": function() {
				var bValid = true;

				var inputs = document.getElementById('registration-form').elements;
                                for(var k = 0; k < inputs.length; k++) {
                                    if (inputs[k].value == "") {
						bValid = false;
						break;
                                    }
                                }
						
				if ($("#registration_email")[0].value != $("#registration_email_repeated")[0].value)
					bValid = false;
				else if ($("#registration_password")[0].value != $("#registration_password_repeated")[0].value)
					bValid = false;
                                				
				if ( bValid ) {
					$("#registration_format_err")[0].style.visibility = "hidden";
					$("#registration_form")[0].submit();
					$(this).dialog( "close" );
				} else {
					$("#registration_format_err")[0].style.visibility = "visible";
				}
			},
			"Prekliči": function() {
				$(this).dialog( "close" );
			}
		},
		close: function() {
			$(this).dialog( "close" );
		}
	});       
});

$(function() {
	$("#tabs").tabs();
});

/* -- MAIN FUNCTIONS --*/
function onLogin(){
    var inputs = document.getElementById('login_form').elements;
    for(var k = 0; k < inputs.length; k++){
    	if(inputs[k].value == ""){
    		$("#format_err")[0].style.visibility = "visible";
			break;
		}
    }
}

function initRegistration(){
    $("#registration_dialog").dialog( "open" );
}

function checkRegistrationEmail(){
    var dom_err = document.getElementById("format_err_registration_email").style;
    var expr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,253}\.[a-zA-Z]{2,4}$/
    if(document.getElementById("registration_email").value.search(expr) == -1 && document.getElementById("registration_email").value != ""){
        dom_err.visibility = "visible";	
    }
    else{
        dom_err.visibility = "hidden";
    }
    updateRegisterSubmitButton();
}

function checkRegistrationPassword(){
    var dom_err = document.getElementById("format_err_registration_password").style;
    var expr = /^[a-zA-Z0-9-@#$%^&+=]{4,30}$/
    if(document.getElementById("registration_password").value.search(expr) == -1 && document.getElementById("registration_password").value != ""){
        dom_err.visibility = "visible";
    } 
    else{
        dom_err.visibility = "hidden";
    }
    updateRegisterSubmitButton();
}

function checkName() {
    var dom_err = document.getElementById("format_err_name").style;
    var expr = /^[a-zA-ZšŠčČžŽ]{2,15}( [a-zA-ZšŠčČžŽ]{2,15})*$/
    if(document.getElementById("name").value.search(expr) == -1 && document.getElementById("name").value != ""){
        dom_err.visibility = "visible";	
    } 
    else{
        dom_err.visibility = "hidden";
    }
    updateRegisterSubmitButton();
}

function checkSurname() {
    var dom_err = document.getElementById("format_err_surname").style;
    var expr = /^[a-zA-ZšŠčČžŽ]{2,15}( [a-zA-ZšŠčČžŽ]{2,15})*$/
    if(document.getElementById("surname").value.search(expr) == -1 && document.getElementById("surname").value != ""){
        dom_err.visibility = "visible";	
    } 
    else{
        dom_err.visibility = "hidden";
    }
    updateRegisterSubmitButton();
}

function checkPlace(){
    var dom_err = document.getElementById("format_err_place").style;
    var expr = /^[a-zA-ZšŠčČžŽ]{2,15}( [a-zA-ZšŠčČžŽ]{2,15})*$/
    if(document.getElementById("place").value.search(expr) == -1 && document.getElementById("place").value != ""){
        dom_err.visibility = "visible";	
    } 
    else{
        dom_err.visibility = "hidden";
    }
    updateRegisterSubmitButton();
}

function updateRegisterSubmitButton(){	
    var valid = ($("#format_err_place")[0].style.visibility == "hidden") &&
                ($("#format_err_name")[0].style.visibility == "hidden") &&
                ($("#format_err_surname")[0].style.visibility == "hidden") &&
                ($("#format_err_registration_email")[0].style.visibility == "hidden") &&
                ($("#format_err_registration_password")[0].style.visibility == "hidden");

    if (valid)
            $('button:eq(0)',$('#registration_dialog').dialog.buttons).button('enable');
    else
            $('button:eq(0)',$('#registration_dialog').dialog.buttons).button('disable');
}

function searchFriends(){
    var name = $("input[name='friend_search']").val();
    
    $.ajax({
        url:"SearchFriends",
        type:"POST",
        data:name,
        success:function(response){
            $('#search_results').html(response);
        }
    });
}
